import {resp} from '@srcUtils/resp';
import {Request, Response} from 'express';
import {Handler} from '@srcTypes/route';
import {AESCipher} from '@srcUtils/utils';
import {lark} from '@/config';


export const getDataFromOther: Handler = (req: Request, res: Response) => {
    // 目前用不到 userId 和 employeeId
    // eslint-disable-next-line no-unused-vars
    const {token, userId, employeeId} = req.body;
    // 测试期间不做token校验
    const localToken = 'abc123456';
    if (localToken !== token) {
        res.json(resp.setCode(123).setMsg('token 不合法，联系开发者获取 token').setData({}).collect());
        return;
    }
    const data = {
        'result': {
            'options': [
                {
                    'id': 'id1',
                    'value': 'test',
                },
                {
                    'id': 'id2',
                    'value': 'name2',
                },
                {
                    'id': 'id3',
                    'value': 'name3',
                },
            ],
            'i18nResources': [
                {
                    'locale': 'zh_cn',
                    'isDefault': true,
                    'texts': {
                        'name1': `来自ip:${lark.env.hostname}的value1`,
                        'name2': `来自ip:${lark.env.hostname}的value2`,
                        'name3': `来自ip:${lark.env.hostname}的value3`,
                    },
                },
                {
                    'locale': 'en_us',
                    'isDefault': false,
                    'texts': {
                        'name1': `this is from ip ${lark.env.hostname}'s value1`,
                        'name2': `this is from ip ${lark.env.hostname}'s value2`,
                        'name3': `this is from ip ${lark.env.hostname}'s value3`,
                    },
                },
            ],
        },
    };
    res.json(resp.setCode(0).setMsg('success').setData(data).collect());
};

export const getDataFromOtherEncode: Handler = (req: Request, res: Response) => {
    // 目前用不到 userId 和 employeeId
    // eslint-disable-next-line no-unused-vars
    const {token, userId, employeeId} = req.body;
    const key = 'mvPhgCMY8wN7WXzs';
    // 测试期间不做token校验
    const localToken = 'abc123456';
    if (localToken !== token) {
        res.json(resp.setCode(500).setMsg('token 不合法，联系开发者获取 token').setData({}).collect());
    }
    // 原始数据
    const sourceData = {
        'code': 0,
        'msg': 'success!',
        'result': {
            'options': [
                {
                    'id': 'id1',
                    'value': 'name1',
                },
                {
                    'id': 'id2',
                    'value': 'name2',
                },
                {
                    'id': 'id3',
                    'value': 'name3',
                },
            ],
            'i18nResources': [
                {
                    'locale': 'zh_cn',
                    'isDefault': true,
                    'texts': {
                        'name1': `来自ip:${lark.env.hostname}的value1`,
                        'name2': `来自ip:${lark.env.hostname}的value2`,
                        'name3': `来自ip:${lark.env.hostname}的value3`,
                    },
                },
                {
                    'locale': 'en_us',
                    'isDefault': false,
                    'texts': {
                        'name1': `this is from ip ${lark.env.hostname}'s value1`,
                        'name2': `this is from ip ${lark.env.hostname}'s value2`,
                        'name3': `this is from ip ${lark.env.hostname}'s value3`,
                    },
                },
            ],
        },
    };
    const dataStr = JSON.stringify(sourceData);
    const cipher = new AESCipher();
    const ret = cipher.encrypt(dataStr, key);
    res.json(resp.setCode(0).setMsg('success').setData(ret).collect());
};
